package com.floreantpos.ui.dialog;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

import org.jdesktop.swingx.JXDatePicker;

import com.floreantpos.main.Application;
import com.floreantpos.model.TipsCashoutReport;
import com.floreantpos.model.User;
import com.floreantpos.model.dao.GratuityDAO;
import com.floreantpos.model.dao.UserDAO;
import com.floreantpos.swing.GlassPane;
import com.floreantpos.swing.ListComboBoxModel;
import com.floreantpos.ui.util.UiUtil;
import com.googlecode.floreo.ecf.bematech.BematechMP2100;

import foxtrot.Job;
import foxtrot.Worker;

/**
 *
 * @author MShahriar
 */
public class ManagerDialog extends JFrame {

  private GlassPane glassPane;

  /**
   * Creates new form ManagerDialog
   */
  public ManagerDialog() {
    super();
    initComponents();

    setIconImage(Application.getPosWindow().getIconImage());
    setTitle(Application.getTitle() + " - " + com.floreantpos.POSConstants.MANAGER_FUNCTION);

    glassPane = new GlassPane();
    setGlassPane(glassPane);
  }

  public void setGlassPaneVisible(boolean b) {
    glassPane.setVisible(b);
  }

  private void initComponents() {
    titlePanel1 = new com.floreantpos.ui.TitlePanel();
    transparentPanel4 = new com.floreantpos.swing.TransparentPanel();
    transparentPanel2 = new com.floreantpos.swing.TransparentPanel();
    transparentPanel3 = new com.floreantpos.swing.TransparentPanel();
    btnShowTips = new com.floreantpos.swing.PosButton();
    btnDrawerPullReport = new com.floreantpos.swing.PosButton();
    btnOpenTickets = new com.floreantpos.swing.PosButton();
    btnClosedTickets = new com.floreantpos.swing.PosButton();
    btnCashDrops = new com.floreantpos.swing.PosButton();
    btnPayout = new com.floreantpos.swing.PosButton();
    transparentPanel1 = new com.floreantpos.swing.TransparentPanel();
    btnFinish = new com.floreantpos.swing.PosButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    titlePanel1.setTitle(com.floreantpos.POSConstants.MANAGER_FUNCTION);
    getContentPane().add(titlePanel1, java.awt.BorderLayout.NORTH);

    transparentPanel4.setLayout(new java.awt.BorderLayout());

    transparentPanel4.setOpaque(true);
    transparentPanel2.setLayout(new java.awt.GridBagLayout());

    transparentPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
    transparentPanel3.setLayout(new java.awt.GridLayout(2, 2, 5, 5));

    btnOpenTickets.setText(com.floreantpos.POSConstants.OPEN_TICKETS);
    btnOpenTickets.setPreferredSize(new java.awt.Dimension(220, 60));
    btnOpenTickets.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnOpenTicketsActionPerformed(evt);
      }
    });

    transparentPanel3.add(btnOpenTickets);

    btnClosedTickets.setText(com.floreantpos.POSConstants.ORDERS_CLOSED);
    btnClosedTickets.setPreferredSize(new java.awt.Dimension(220, 60));
    btnClosedTickets.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnClosedTicketsActionPerformed(evt);
      }
    });

    transparentPanel3.add(btnClosedTickets);

    btnShowTips.setText(com.floreantpos.POSConstants.SERVER_TIPS);
    btnShowTips.setPreferredSize(new java.awt.Dimension(220, 60));
    btnShowTips.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        doShowServerTips(evt);
      }
    });

    transparentPanel3.add(btnShowTips);

    btnPayout.setText(com.floreantpos.POSConstants.CAPITAL_PAY_OUT);
    btnPayout.setPreferredSize(new java.awt.Dimension(220, 60));
    btnPayout.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnPayoutActionPerformed(evt);
      }
    });

    transparentPanel3.add(btnPayout);

    btnCashDrops.setText(com.floreantpos.POSConstants.DRAWER_BLEED);
    btnCashDrops.setPreferredSize(new java.awt.Dimension(220, 60));
    btnCashDrops.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnCashDropsActionPerformed(evt);
      }
    });

    transparentPanel3.add(btnCashDrops);

    btnDrawerPullReport.setText(com.floreantpos.POSConstants.DRAWER_PULL);
    btnDrawerPullReport.setPreferredSize(new java.awt.Dimension(220, 60));
    btnDrawerPullReport.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnDrawerPullReportActionPerformed(evt);
      }
    });

    transparentPanel3.add(btnDrawerPullReport);

    transparentPanel2.add(transparentPanel3, new java.awt.GridBagConstraints());

    transparentPanel4.add(transparentPanel2, java.awt.BorderLayout.CENTER);

    transparentPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

    btnFinish.setText(com.floreantpos.POSConstants.FINISH);
    btnFinish.setPreferredSize(new java.awt.Dimension(120, 60));
    btnFinish.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnFinishActionPerformed(evt);
      }
    });

    transparentPanel1.add(btnFinish);

    transparentPanel4.add(transparentPanel1, java.awt.BorderLayout.SOUTH);

    getContentPane().add(transparentPanel4, java.awt.BorderLayout.CENTER);

    pack();
  }

  private void btnFinishActionPerformed(java.awt.event.ActionEvent evt) {
    dispose();
  }

  private void btnPayoutActionPerformed(java.awt.event.ActionEvent evt) {
    PayoutDialog dialog = new PayoutDialog(Application.getPosWindow(), true);
    dialog.open();
  }

  private void btnCashDropsActionPerformed(java.awt.event.ActionEvent evt) {
    Job job = new Job() {
      @Override
      public Object run() {
        try {
          setGlassPaneVisible(true);
          CashDropDialog dialog = new CashDropDialog(ManagerDialog.this);
          dialog.initDate();
          dialog.open();
        } catch (Exception e) {
          POSMessageDialog.showError(com.floreantpos.POSConstants.ERROR_MESSAGE, e);
        } finally {
          setGlassPaneVisible(false);
        }
        return null;
      }
    };
    Worker.post(job);
  }

  private void btnOpenTicketsActionPerformed(java.awt.event.ActionEvent evt) {
    Job job = new Job() {
      @Override
      public Object run() {
        try {
          setGlassPaneVisible(true);
          OpenTicketsListDialog dialog = new OpenTicketsListDialog(ManagerDialog.this, true);
          dialog.open();
        } catch (Exception e) {
          POSMessageDialog.showError(com.floreantpos.POSConstants.ERROR_MESSAGE, e);
        } finally {
          setGlassPaneVisible(false);
        }
        return null;
      }
    };
    Worker.post(job);
  }

  private void btnClosedTicketsActionPerformed(java.awt.event.ActionEvent evt) {
    Job job = new Job() {
      @Override
      public Object run() {
        try {
          setGlassPaneVisible(true);
          ClosedTicketsListDialog dialog = new ClosedTicketsListDialog(ManagerDialog.this, true);
          dialog.open();
        } catch (Exception e) {
          POSMessageDialog.showError(com.floreantpos.POSConstants.ERROR_MESSAGE, e);
        } finally {
          setGlassPaneVisible(false);
        }
        return null;
      }
    };
    Worker.post(job);
  }

  private void btnDrawerPullReportActionPerformed(java.awt.event.ActionEvent evt) {
    showDrawerPullReport();
  }

  private void showDrawerPullReport() {
    Job job = new Job() {
      @Override
      public Object run() {
        try {
          setGlassPaneVisible(true);
          DrawerPullReportDialog dialog = new DrawerPullReportDialog(ManagerDialog.this, true);
          dialog.setTitle(com.floreantpos.POSConstants.DRAWER_PULL);
          dialog.initialize();
          dialog.setSize(430, 500);
          dialog.setResizable(false);
          dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
          dialog.open();
        } catch (Exception e) {
          POSMessageDialog.showError(com.floreantpos.POSConstants.ERROR_MESSAGE, e);
        } finally {
          setGlassPaneVisible(false);
        }
        return null;
      }
    };
    Worker.post(job);
  }

  private void doShowServerTips(java.awt.event.ActionEvent evt) {

    Job job = new Job() {
      @Override
      public Object run() {
        try {
          setGlassPaneVisible(true);

          JPanel panel = new JPanel(new MigLayout());
          List<User> users = UserDAO.getInstance().findAll();

          JXDatePicker fromDatePicker = UiUtil.getCurrentMonthStart();
          JXDatePicker toDatePicker = UiUtil.getCurrentMonthEnd();

          panel.add(new JLabel(com.floreantpos.POSConstants.SELECT_USER + ":"), "grow");
          JComboBox userCombo = new JComboBox(new ListComboBoxModel(users));
          panel.add(userCombo, "grow, wrap");
          panel.add(new JLabel(com.floreantpos.POSConstants.FROM + ":"), "grow");
          panel.add(fromDatePicker, "wrap");
          panel.add(new JLabel(com.floreantpos.POSConstants.TO_), "grow");
          panel.add(toDatePicker);

          int option = JOptionPane.showOptionDialog(ManagerDialog.this, panel, com.floreantpos.POSConstants.SELECT_CRIETERIA, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
          if (option != JOptionPane.OK_OPTION) {
            return null;
          }

          GratuityDAO gratuityDAO = new GratuityDAO();
          TipsCashoutReport report = gratuityDAO.createReport(fromDatePicker.getDate(), toDatePicker.getDate(), (User) userCombo.getSelectedItem());

          TipsCashoutReportDialog dialog = new TipsCashoutReportDialog(report, ManagerDialog.this, true);
          dialog.setSize(400, 600);
          dialog.open();
        } catch (Exception e) {
          POSMessageDialog.showError(com.floreantpos.POSConstants.ERROR_MESSAGE, e);
        } finally {
          setGlassPaneVisible(false);
        }
        return null;
      }
    };
    Worker.post(job);
  }

  public void open() {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    int width = 800;
    int height = 600;

    int x = (screenSize.width - 800) / 2;
    int y = (screenSize.height - 600) / 2;

    setSize(width, height);

    setLocation(x, y);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setVisible(true);
  }

  private com.floreantpos.swing.PosButton btnDrawerPullReport;
  private com.floreantpos.swing.PosButton btnCashDrops;
  private com.floreantpos.swing.PosButton btnFinish;
  private com.floreantpos.swing.PosButton btnOpenTickets;
  private com.floreantpos.swing.PosButton btnClosedTickets;
  private com.floreantpos.swing.PosButton btnShowTips;
  private com.floreantpos.swing.PosButton btnPayout;
  private com.floreantpos.ui.TitlePanel titlePanel1;
  private com.floreantpos.swing.TransparentPanel transparentPanel1;
  private com.floreantpos.swing.TransparentPanel transparentPanel2;
  private com.floreantpos.swing.TransparentPanel transparentPanel3;
  private com.floreantpos.swing.TransparentPanel transparentPanel4;

}
